#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include "cl.h"
#include "client.h"
#include "../scanADT/scanadt.h"
#include "../scanADT/strlib.h"


static void
help(void)
{
	char msg[] =
	"user <user> -- Determines the name with which the user identifies "
						"when he enters a chat session\n"
	"sessions -- Shows a list of the active sessions\n"
	"list <session> -- Shows a list of the users participating in the "
						"required session, or a message that it doesn't exist\n"
	"enter <session> -- Enters an existing session or creates a new one\n"
	"help -- Shows this help\n"
	"quit -- Quits the chat session, in chat mode, or the program\n"
	"\n";

	printw("%s", msg);
	refresh();
}

/* get input line */
int
input(char * prompt, char * inpbuf)
{
	int c, x=0, y=0;
	int count= 0;

	/* print prompt */
	printw("%s", prompt);
	refresh();

	forever
	{
		c = getch();

		if(c==KEY_BACKSPACE)
		{
			if(count > 0)
			{
				inpbuf[--count] = '\0';
				getyx(stdscr, y, x);
				--x;
				wmove(stdscr, y, x);
				wdelch(stdscr);
				wrefresh(stdscr);
			}
		}
		if( count < MAXLINE && c!=KEY_BACKSPACE)
		{
			inpbuf[count++]= c;
			waddch(stdscr, c);
			wrefresh(stdscr);
		}
		if( c == '\n' && count < MAXLINE )
		{
			inpbuf[count]= '\0';
			return count;
		}
		if( c == '\n')
		{
			printw("\nInput line too long (Max: 511 characters).\n");
			printw("%s", prompt);
			refresh();
			count= 0;
		}
	}
}

/* process input line */
int
process_line(char * line)
{
	int i, narg= 0;
	char * arg[MAXARGS+1];
	char * token= NULL;
	scannerADT scanner;

	if( checkLine(line) )
	{
		printw("chat: no such command\n");
		refresh();
		return 0;
	}
	scanner= NewScanner();
	SetScannerString(scanner, line);

	while( MoreTokensExist(scanner) )
	{
		token= ReadToken(scanner);
		if( (arg[narg]= malloc(strlen(token))) == NULL )
		{
			freeArg(arg, narg-1);
			return 1;
		}
		strcpy(arg[narg++], token);
	}
	if( (i= process_command(narg, arg)) == 0 )
	{
		printw("chat: no such command\n");
		refresh();
	}
	else if( i == EQUIT || i < 0 )
	{
		freeArg(arg, narg);
		return 1;
	}
	FreeScanner(scanner);
	freeArg(arg, narg);
	return 0;
}

int
checkLine(char * line)
{
	int count= 0;

	while( *line != '\0' )
	{
		if( count != 0 && *line == '"' && count%2 == 0 && *(line-1) != ' ' &&
		   *(line-1) != '\t')
			return 1;
		if( *line == '"' && count%2 != 0 && *(line+1) != ' ' &&
		   *(line+1) != '\n' && *(line+1) != '\t' )
			return 1;
		if( *line == '"' )
			count++;
		line++;
	}
	if( count%2 == 0 )
		return 0;
	return 1;
}

int
process_command(int argc, char * argv[])
{
	if( !argc )					/* If there are no arguments, do nothing */
		return 1;

	if (strcmp(STRHELP, argv[0]) == 0 ) 			/* help */
	{
		if (argc != 1)
		{
			printw("For help type \"%s\"", STRHELP);
			refresh();
			return 1;
		}
		help();
		return EHELP;
	}

	if (strcmp(STRQUIT, argv[0]) == 0)				/* quit */
	{
		if (argc != 1)
		{
			printw("Usage: %s \n", STRQUIT);
			refresh();
			return 1;
		}
		close_session();

		return EQUIT;
	}
	 if (strcmp(STRUSR, argv[0]) == 0)			/* user */
	{
		if (argc != 2)
		{
			printw("Usage: %s <username>\n", STRUSR);
			refresh();
			return 1;
		}
		setNick(argv[1]);
		return EUSR;
	}
	else if (strcmp(STRSESS, argv[0]) == 0)			/* sessions */
	{
		if (argc != 1)
		{
			printw("Usage: %s\n", STRSESS);
			refresh();
			return 1;
		}
		listSessions();
		return ESESS;
	}
	else if (strcmp(STRLIST , argv[0]) == 0)		/* list */
	{
		if (argc != 2)
		{
			printw("Usage: %s <session>\n", STRLIST);
			refresh();
			return 1;
		}
		listUsers(argv[1]);
		return ELIST;
	}
	else if (strcmp(STRENT , argv[0]) == 0)			/* ingresar */
	{
		if (argc != 2)
		{
			printw("Usage: %s <session>\n", STRENT);
			refresh();
			return 1;
		}

		enter(argv[1]);
		return EENT;
	}
	return 0;
}


void
freeArg(char ** arg, int narg)
{
	int i;

	if( narg < 1 )
		return;
	for( i= 0; i < narg; i++ )
		free(arg[i]);
	return;
}

